import Taro from "@tarojs/taro";
import {API_CampusRoot} from "./baseUrl";
import interceptors from "./interceptors";
import {Method, Options} from "@/network/type";
import getInfo from "../pages/noticeApp/utils/getInfo";

interceptors.forEach((interceptorItem) => Taro.addInterceptor(interceptorItem));


class httpRequest {
    baseOptions(params, method:Method = "GET", ...rest) {
      let {url, data, header, autoProxy=true, loading=false} = params;
      let definedHeader = header || {
         "content-type": "application/json;charset=UTF-8",
         // 'content-type': "application/x-www-form-urlencoded",
         // 'Authorization':Taro.getStorageSync('Authorization')
      };
      const option:Options = {
         url, //地址
         data: data, //传参
         method: method, //请求方式
         timeout: 50000, // 超时时间
         header: definedHeader,
         autoProxy,//控制是否走代理（已根据编译环境统一处理，本地打包环境一致）
         loading,//控制请求是否提示加载
         ...rest
      };
      return new Promise((resolve, reject) => {
         Taro.request(option).then(resolve).catch(reject);
      });
   }

   get<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let option = {url, data, autoProxy:false, header, loading};
      return this.baseOptions(option) as Promise<T>;
   }

   post<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let params = {url, data, autoProxy:false, header, loading};
      return this.baseOptions(params, "POST") as Promise<T>;
   }

   put<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let option = {url, data, autoProxy:false, header, loading};
      return this.baseOptions(option, "PUT") as Promise<T>;
   }

   delete<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let option = {url, data, autoProxy:false, header, loading};
      return this.baseOptions(option, "DELETE") as Promise<T>;
   }

   transmitGet<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let option = {url, data, header, loading};
      return this.baseOptions(option) as Promise<T>;
   }

   transmitPost<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let params = {url, data, header, loading};
      return this.baseOptions(params, "POST") as Promise<T>;
   }

   transmitPut<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let option = {url, data, header, loading};
      return this.baseOptions(option, "PUT") as Promise<T>;
   }

   transmitDelete<T = any>(url, data={}, header:any='', loading=false):Promise<T> {
      let option = {url, data, header, loading};
      return this.baseOptions(option, "DELETE") as Promise<T>;
   }

   //校园通代理form表单请求
   transmitFormData(data = {}, loading=false) {
      const option:Options = {
         url: `${API_CampusRoot}/transmit/doPostForm`, //地址
         data: data, //传参
         method: 'POST', //请求方式
         timeout: 50000, // 超时时间
         header: {
            "content-type": "application/json;charset=UTF-8",
         },
         loading:loading//可自定义拦截器参数，此loading代表不显示loading框
      };
      return new Promise((resolve, reject) => {
         Taro.request(option).then(resolve).catch(reject);
      })
   }


}

export default new httpRequest();
